package com.example.demo.service;

import com.example.demo.entity.StudentCourseProgress;
import com.example.demo.mapper.StudentCourseProgressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Optional;

@Service
public class StudentCourseProgressService {

    @Autowired
    private StudentCourseProgressMapper progressMapper; // 确保 Mapper 已定义且注入成功

    // 创建初始进度记录：参数类型适配 Entity（均为String类型）
    public StudentCourseProgress createInitialProgress(String studentId, String courseId) {
        // 校验参数（String类型需判断空串）
        if (studentId == null || studentId.trim().isEmpty() || courseId == null || courseId.trim().isEmpty()) {
            throw new IllegalArgumentException("学生ID和课程ID不能为空");
        }
        // 检查是否已存在进度记录（Mapper方法参数同步为String类型）
        if (progressMapper.findByStudentAndCourse(studentId, courseId).isPresent()) {
            throw new IllegalArgumentException("进度记录已存在");
        }
        // 初始化进度：赋值类型与Entity完全匹配
        StudentCourseProgress progress = new StudentCourseProgress();
        progress.setStudentId(studentId); // String类型
        progress.setCourseId(courseId);   // String类型
        progress.setCompletedLessons(0);
        progress.setProgressRate(BigDecimal.ZERO);
        progress.setCompletionStatus("NOT_STARTED");
        progress.setLastStudyTime(LocalDateTime.now());
        progress.setUpdatedAt(LocalDateTime.now());

        if (progressMapper.insert(progress) > 0) {
            return progress;
        } else {
            throw new RuntimeException("进度记录创建失败");
        }
    }

    // 查询学生课程进度：参数保持String类型（与Entity匹配）
    public Optional<StudentCourseProgress> findProgress(String courseId, String studentId) {
        // 校验参数
        if (studentId == null || studentId.trim().isEmpty() || courseId == null || courseId.trim().isEmpty()) {
            throw new IllegalArgumentException("学生ID和课程ID不能为空");
        }
        // 直接使用String类型参数调用Mapper（无需类型转换）
        return progressMapper.findByStudentAndCourse(studentId, courseId);
    }

    // 更新进度：courseId已改为String类型
    public void updateProgress(StudentCourseProgress progress) {
        // 校验核心参数
        if (progress.getStudentId() == null || progress.getStudentId().trim().isEmpty()) {
            throw new IllegalArgumentException("学生ID不能为空");
        }
        if (progress.getCourseId() == null || progress.getCourseId().trim().isEmpty()) {
            throw new IllegalArgumentException("课程ID不能为空");
        }
        // 校验进度范围（0-100）
        BigDecimal progressRate = progress.getProgressRate();
        if (progressRate == null || progressRate.compareTo(BigDecimal.ZERO) < 0 || progressRate.compareTo(new BigDecimal(100)) > 0) {
            throw new IllegalArgumentException("进度值必须在0-100之间");
        }
        // 校验记录是否存在
        Optional<StudentCourseProgress> existing = progressMapper.findByStudentAndCourse(
                progress.getStudentId(), progress.getCourseId() // 均为String类型
        );
        if (existing.isEmpty()) {
            throw new IllegalArgumentException("进度记录不存在");
        }
        // 更新必要字段
        progress.setId(existing.get().getId()); // 复用已有主键ID
        progress.setLastStudyTime(LocalDateTime.now());
        progress.setUpdatedAt(LocalDateTime.now());

        if (progressMapper.update(progress) <= 0) {
            throw new RuntimeException("进度更新失败");
        }
    }
}